package eu.codlab.twitteirb;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.ByteOrder;

import eu.codlab.twitteirb.manage.SendTweetListener;

import android.content.Context;
import android.os.AsyncTask;
//import android.util.Log;

public final class MessageManager implements SendTweetListener{
	private TwittEirbService _master;
	private Context _context;
	private String _last;
	private String _cookie;

	private MessageManager(){
		_last = null;
	}

	public MessageManager(Context context,TwittEirbService master, CookieManager cookie){
		this();
		_master = master;
		_cookie = "";
		_context = context;
	}

	public void setLastId(String last){
		//Log.d("last",""+last);
		_last =last;
	}

	private final class MessageAsync extends AsyncTask<URL, Object, Object>{
		private MessageAsync(){

		}

		@Override
		protected Integer doInBackground(URL... arg0) {
			try{
				URL url = arg0[1];
				HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
				urlConnection.setRequestProperty("Cookie", _cookie+";");

				//_cookie.setCookies(urlConnection);
				//InputStream in = new BufferedInputStream(urlConnection.getInputStream());
				urlConnection.connect();
				InputStream in = new BufferedInputStream(urlConnection.getInputStream());
				//Log.d("obtenu",_cookie+" "+url+" "+urlConnection.getURL().getHost());
				if (!url.getHost().equals(urlConnection.getURL().getHost())) {
					//_master.onGetMessageRedirect();
				}else{
					_master.onGetLoginString(readStream(in));
				}
				urlConnection.disconnect();
				
				url = arg0[0];
				urlConnection = (HttpURLConnection) url.openConnection();
				urlConnection.setRequestProperty("Cookie", _cookie+";");

				//_cookie.setCookies(urlConnection);
				//InputStream in = new BufferedInputStream(urlConnection.getInputStream());
				urlConnection.connect();
				in = new BufferedInputStream(urlConnection.getInputStream());
				//Log.d("obtenu",_cookie+" "+url+" "+urlConnection.getURL().getHost());
				if (!url.getHost().equals(urlConnection.getURL().getHost())) {
					_master.onGetMessageRedirect();
				}else{
					_master.onGetMessageString(readStream(in));
				}
				urlConnection.disconnect();
			} catch (Exception e) {
				e.printStackTrace();
			}
			return null;
		}

	}

	public void go(String cookie){
		_cookie = cookie;
		go();
	}

	String getCookie(){
		return _cookie;
	}
	
	public void go(){
		URL url=null;
		try {

			if(_last == null){
				url = new URL("http://twitt.eirb.fr/?ajax");
			}else{
				url = new URL("http://twitt.eirb.fr/?ajax&minid="+_last);
			}
			MessageAsync m = new MessageAsync();
			URL[] u = new URL[2];
			u[0]=url;
			u[1]=new URL("http://twitt.eirb.fr/online.php");
			m.execute(u);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	public static String readStream(InputStream in) throws IOException {  
		StringBuilder sb = new StringBuilder();  
		BufferedReader r = new BufferedReader(new InputStreamReader(in),2048);  
		for (String line = r.readLine(); line != null; line =r.readLine()){  
			sb.append(line);  
		}  
		in.close();  
		return sb.toString();  
	}

	public void reset() {
		this._cookie="N;";
	}

	@Override
	public void onTweetSend() {
		go();
	}   


}
